package com.boarsoft.common.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.boarsoft.common.Util;

public class ObjConvertUtil {
	interface Converter {
		Object convert(Object obj);
	}

	public static final Map<String, Converter> converterMap = new HashMap<>();

	public static final Converter string2bigdecimal = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return new BigDecimal((String) obj);
				}
			};
	public static final Converter number2bigdecimal = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return BigDecimal.valueOf(((Number) obj).doubleValue());
				}
			};
	public static final Converter string2integer = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return Integer.parseInt(String.valueOf(obj));
				}
			};
	public static final Converter string2double = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return Double.parseDouble(String.valueOf(obj));
				}
			};
	public static final Converter string2float = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return Float.parseFloat(String.valueOf(obj));
				}
			};
	public static final Converter string2long = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return Long.parseLong(String.valueOf(obj));
				}
			};
	public static final Converter string2date = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return Util.str2date(String.valueOf(obj), Util.STDDF2);
				}
			};
	public static final Converter float2string = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return String.valueOf(obj);
				}
			};
	public static final Converter double2string = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return String.valueOf(obj);
				}
			};
	public static final Converter integer2string = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return String.valueOf(obj);
				}
			};
	public static final Converter long2string = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return String.valueOf(obj);
				}
			};
	public static final Converter bigdecimal2string = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return String.valueOf(obj);
				}
			};
	public static final Converter date2string = //
			new Converter() {
				@Override
				public Object convert(Object obj) {
					return Util.date2str((Date) obj, Util.STDDF);
				}
			};

	static {
		// String
		converterMap.put("java.lang.String to java.math.BigDecimal", string2bigdecimal);
		// Number
		converterMap.put("java.lang.Integer to java.math.BigDecimal", number2bigdecimal);
		converterMap.put("java.lang.Double to java.math.BigDecimal", number2bigdecimal);
		converterMap.put("java.lang.Float to java.math.BigDecimal", number2bigdecimal);
		converterMap.put("java.lang.Long to java.math.BigDecimal", number2bigdecimal);
		converterMap.put("java.lang.Short to java.math.BigDecimal", number2bigdecimal);
		converterMap.put("java.lang.Byte to java.math.BigDecimal", number2bigdecimal);
		// String 转其它
		converterMap.put("java.lang.String to java.lang.Integer", string2integer);
		converterMap.put("java.lang.String to java.lang.Double", string2double);
		converterMap.put("java.lang.String to java.lang.Float", string2float);
		converterMap.put("java.lang.String to java.lang.Long", string2long);
		converterMap.put("java.lang.String to java.util.Date", string2date);
		// 其它转String
		converterMap.put("java.lang.Integer to java.lang.String", integer2string);
		converterMap.put("java.lang.Double to java.lang.String", double2string);
		converterMap.put("java.lang.Float to java.lang.String", float2string);
		converterMap.put("java.lang.Long to java.lang.String", long2string);
		converterMap.put("java.util.Date to java.util.String", date2string);
	}

	/**
	 *
	 * @param key
	 *            目标类型
	 * @param obj
	 *            原数据
	 * @return
	 */
	public static Object convert(String key, Object obj) {
		if (obj == null) {
			return obj;
		}
		if (key.equals(obj.getClass().getName())) {
			return obj;
		}
		Converter oc = converterMap.get(obj.getClass().getName().concat(" to ").concat(key));
		if (oc == null) {
			return obj;
		}
		return oc.convert(obj);
	}

	public static void main(String[] args) {
		Object convert = convert("java.lang.Byte", 11);
		System.out.println(convert + "---" + convert.getClass().getName());
		Object convert1 = convert("java.util.Date", "10110101");
		System.out.println(convert1 + "---" + convert1.getClass().getName());
	}
}